<?php

if (!class_exists("sthook_2_1")) {

    class sthook_2_1 {

        public $option = '';

        public function __construct($config) {
            $this->config = $config;
            //$this->config ['pluginDir'] = WP_PLUGIN_DIR . '/' . $config ['plugin_name'] . '/';
            $this->option = $this->get_option($this->config['plugin_name'] . '_' . $this->config['option_key'] . '_settings');
            $this->hookManage();
            $this->hookCustom();
        }

        protected function getOption($key, $user_id = 0) {
            $option = $this->get_option($this->config['plugin_name'] . '_' . $this->config['option_key'] . '_settings', $user_id);
            return $option[$key];
        }

        public function loadmodel($modelName, $dir = '') {
            $className = $modelName . "_model";
            $dir = empty($dir) ? $this->config ['pluginDir'] : $dir;
            $fileName = $dir . "models/{$modelName}.php";
            require_once $fileName;
            $obj = new $className($this->config);
            return $obj;
        }

        public function hookManage() {

            $method = array($this, 'loadTextdomain');
            add_action('init', $method);
            $method = array($this, 'languageStart');
            add_filter('load_textdomain_mofile', $method, 100, 2);

            $method = array($this, 'includeExtraPlugin');
            add_action($this->config['plugin_name'] . '_extra_contruction', $method);

            $method = array($this, 'actionTitle');
            add_filter('wp_title', $method, 100, 3);
            
        }

        public function hookCustom() {
            //
        }

        public function includeExtraPlugin() {
            
        }

        //Initialize hook function
        public function loadTextdomain() {
            load_plugin_textdomain($this->config['plugin_name'], false, $this->config['plugin_name'] . '/languages');
        }

        public function languageStart($mofile, $domain) {
            global $locale;

            //$fbcontent_settings = get_option('fbcontent_settings');
            $language = $this->option['st_language'];

            if ($domain == $this->config['plugin_name']) {
                $mofile = str_replace($locale, $language, $mofile);
                return $mofile;
            }
            return $mofile;
        }

        /**
         * Prints title of action page.
         *
         * @since PluginExpert 1.0
         */
        public function actionTitle($title, $sep, $seplocation) {
            //bloginfo('name');
            if ($title == '')
                $title = get_bloginfo('name');
            return $title;
        }

        function get_option($option, $user_id = 0, $default = false) {
            global $wpdb;

            if (empty($this->config['tableOptions'])) {
                $optionTable = $wpdb->options;
                $where = 'option_name = %s LIMIT 1';
            } else {
                $optionTable = $this->config['tableOptions'];
                if ($user_id == 0)
                    $user_id = get_current_user_id();
                $where = "user_id = $user_id AND option_name = %s LIMIT 1";
            }

            $option = trim($option);
            if (empty($option))
                return false;

            if (defined('WP_SETUP_CONFIG'))
                return false;
            $value = wp_cache_get($option, 'options');
            if (false === $value) {
                $suppress = $wpdb->suppress_errors();
                $row = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM $optionTable WHERE {$where} ", $option));
                $wpdb->suppress_errors($suppress);
                if (is_object($row)) {
                    $value = $row->option_value;
                    wp_cache_add($option, $value, 'options');
                }else
                    return $default;
            }

            return apply_filters('option_' . $option, maybe_unserialize($value));
        }

        function wp_load_alloptions() {
            global $wpdb;
            if (empty($this->config['tableOptions'])) {
                $optionTable = $wpdb->options;
                $where1 = "WHERE autoload = 'yes'";
                if (!defined('WP_INSTALLING') || !is_multisite()) {
                    $alloptions = wp_cache_get('alloptions', 'options');
                }
                else
                    $alloptions = false;
            } else {
                $optionTable = $this->config['tableOptions'];
                $user_id = get_current_user_id();
                $where1 = "WHERE user_id = $user_id AND autoload = 'yes'";
                $where2 = "WHERE user_id = $user_id";
                $alloptions = false;
            }


            if (!$alloptions) {
                $suppress = $wpdb->suppress_errors();
                if (!$alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $optionTable $where1 "))
                    $alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $optionTable $where2");
                $wpdb->suppress_errors($suppress);
                $alloptions = array();
                foreach ((array) $alloptions_db as $o)
                    $alloptions[$o->option_name] = $o->option_value;
                if (!defined('WP_INSTALLING') || !is_multisite())
                    wp_cache_add('alloptions', $alloptions, 'options');
            }
            //var_dump($alloptions);
            return $alloptions;
        }

        /*
         * This function help you get all option of current user
         * and another option but not include option of another users
         */

        public function getFullOptions() {
            $option = get_option($this->config['plugin_name'] . '_' . $this->config['option_key'] . '_settings');
            if ($this->option)
                return array_merge($option, $this->option);
            else
                return $option;
        }

        
    }

}
?>